import { createRouter, createWebHashHistory } from "vue-router";
import cookie from "../utils/tool";
const routes = [
  {
    path: "/",
    redirect: "/home",
  },
  {
    path: "/home",
    component: () => import("../views/Home.vue"),
    children: [
      {
        path: "/home",
        redirect: "/home/index",
      },
      {
        path: "/home/index",
        component: () => import("../views/home/Index.vue"),
      },
      {
        path: "/home/consult",
        component: () => import("../views/home/Consult.vue"),
      },
      {
        path: "/home/question",
        component: () => import("../views/home/Question.vue"),
      },
      {
        path: "/home/message",
        component: () => import("../views/home/Message.vue"),
      },
      {
        path: "/home/mine",
        component: () => import("../views/home/Mine.vue"),
      },
    ],
  },
  {
    path: "/detail",
    component: () => import("../views/Detail.vue"),
  },
  {
    path: "/login",
    component: () => import("../views/Login.vue"),
  },

  {
    path: "/pour",
    component: () => import("../views/Pour.vue"),
  },
  {
    path: "/article",
    component: () => import("../views/Article.vue"),
  },
  {
    path: "/ststion",
    component: () => import("../views/Station.vue"),
  },
  {
    path: "/register",
    component: () => import("../views/Register.vue"),
  },
  {
    path: "/quit",
    component: () => import("../views/Quit.vue"),
  },
  {
    path: "/consultdet/:id",
    component: () => import("../views/Consultdet.vue"),
    props: true,
    meta: {
      querylogin: true,
    },
  },
  {
    path: "/heart",
    component: () => import("../views/Heart.vue"),
  },
  {
    path: "/chat",
    component: () => import("../views/Chat.vue"),
  },
  {
    path: "/:pathMatch(.*)*",
    component: () => import("../views/Notfound.vue"),
  },
  {
    path: "/account",
    component: () => import("../views/Account.vue"),
  },
  {
    path: "/introduce",
    component: () => import("../views/Introduce.vue"),
  },
  {
    path: "/helpcenter",
    component: () => import("../views/HelpCenter.vue"),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});
// 进入咨询详情页面的路由守卫函数
router.beforeEach((to, from, next) => {
  if (to.meta.querylogin) {
    if (!cookie.get("token")) {
      next("/login");
    } else {
      next();
    }
  } else {
    next();
  }
});
export default router;
